import Cookies from 'js-cookie'

const TokenKey = 'Token'
const TokenAdmin = 'account'
const TokenPassword = 'password'

/**
 * @description 获取单点登录token
 */
export function getToken() {
  return Cookies.get(TokenKey)
}

/**
 * @description 设置单点登录token
 * @param {*} token
 */
export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

/**
 * 移除单点登录token
 */
export function removeToken() {
  return Cookies.remove(TokenKey)
}

/**
 * 截取jwttoken中段信息，转换为json数据
 * @param token
 * @return {*}
 */
export function getDataFromJwtToken() {
  const token = Cookies.get(TokenKey)
  if (!token) {
    return null
  }
  var data = token.split('.')[1]
  if (!data) {
    return null
  }
  return JSON.parse(window.atob(data))
}

/**
 * 存取用户名
 *
 * **/
export function getAdmin() {
  return Cookies.get(TokenAdmin)
}

/**
 * 获取用户名
 * **/
export function setAdmin(account) {
  return Cookies.set(TokenAdmin, account, { expires: 30 })
}

/**
 * 清楚
 * **/
export function removeAdmin() {
  return Cookies.remove(TokenAdmin)
}

/**
 * 存取密码
 *
 * **/
export function getPassword() {
  return Cookies.get(TokenPassword)
}

/**
 * 获取用户名
 * **/
export function setPassword(password) {
  return Cookies.set(TokenPassword, password, { expires: 30 })
}

/**
 * 清楚
 * **/
export function removePassword() {
  return Cookies.remove(TokenPassword)
}
